﻿using System;
using Autofac;
using ComputerVision.BaseForm;
using ComputerVision.Common;
using ComputerVision.Data;
using ComputerVision.PhieuDieuTraCoSoForm;
using ComputerVision.Services;
using ComputerVision.Services.User;
using QuanLyThongTinCoSo.Common;

namespace ComputerVision.PhongForm
{
    public partial class frmPhongDetail : frmDetail
    {
        private readonly IService<Phong> _phongService;
        private readonly IUserService<aspnet_Users> _userService;
        private frmPhieuDieuTraCoSoDetail _frmPhieuDieuTraCs;
        public frmPhongDetail()
        {
            InitializeComponent();
            _phongService = Program.Container.Resolve<IService<Phong>>();
            _userService = Program.Container.Resolve<IUserService<aspnet_Users>>();
            Text = @"Thêm mới Phòng";
            txtSo.Text = Constants.SoDetail.TenSo;
            txtMaPhong.Focus();
        }
        
        public frmPhongDetail(frmPhieuDieuTraCoSoDetail frm)
        {
            InitializeComponent();
            this._frmPhieuDieuTraCs = frm;
            _phongService = Program.Container.Resolve<IService<Phong>>();
            _userService = Program.Container.Resolve<IUserService<aspnet_Users>>();
            Text = @"Thêm mới Phòng";
            txtSo.Text = Constants.SoDetail.TenSo;
            txtMaPhong.Focus();
        }
        
        public frmPhongDetail(Guid id)
        {
            InitializeComponent();
            _phongService = Program.Container.Resolve<IService<Phong>>();
            _userService = Program.Container.Resolve<IUserService<aspnet_Users>>();
            Text = @"Sửa Phòng";
            txtSo.Text = Constants.SoDetail.TenSo;
            txtMaPhong.Focus();

            _fromtype = Constants.TypeForm.Edit;
            guid = id;

            var item = _phongService.Detail(id);
            if (item != null)
            {
                txtTenPhong.Text = item.TenPhong;
                txtMaPhong.Text = item.Ma;
                txtDienGiai.Text = item.DienGiai;
            }
        }

        #region Validation

        protected override bool Validator()
        {
            bool flag = true;

            errorProvider.Dispose();

            if (string.IsNullOrEmpty(txtMaPhong.Text))
            {
                errorProvider.SetError(txtMaPhong, "Bạn chưa nhập mã phòng. Xin vui lòng kiểm tra lại");
                flag = false;
            }

            if (string.IsNullOrEmpty(txtTenPhong.Text))
            {
                errorProvider.SetError(txtTenPhong, "Bạn chưa nhập tên phòng. Xin vui lòng kiểm tra lại");
                flag = false;
            }

            return flag;
        }

        #endregion

        #region Insert
        protected override void Insert()
        {
            try
            {
                var model = new Phong
                {
                    Id = Guid.NewGuid(),
                    TenPhong = txtTenPhong.Text,
                    Ma = txtMaPhong.Text,
                    DienGiai = txtDienGiai.Text,
                    Created = DateTime.Now,
                    CreatedBy_Id = Constants.CurrentUser,
                    Modified = DateTime.Now,
                    ModifiedBy_Id = Constants.CurrentUser
                };
                _phongService.Create(model);

                if (this._frmPhieuDieuTraCs != null)
                {
                    this._frmPhieuDieuTraCs .LoadPhong();
                }
            }
            catch (Exception ex)
            {
                AlertList.Error("Lỗi phát sinh khi tạo mới dữ liệu.");
            }
        }
        #endregion

        #region Edit

        protected override void Edit()
        {
            var item = _phongService.Detail(guid);
            if (item != null)
            {
                item.TenPhong = txtTenPhong.Text;
                item.Ma = txtMaPhong.Text;
                item.DienGiai = txtDienGiai.Text;
                item.Modified = DateTime.Now;
                item.ModifiedBy_Id = Constants.CurrentUser;
                _phongService.Update(item);
            }
        }

        #endregion
    }
}
